package web_type

import (
	"fmt"
	"github.com/gogf/gf/frame/g"
	"regexp"
	"strings"
	"sync"

	"main/config"
	"main/utils"
)

func init() {
	toml, err := config.NewConfigToml()
	if err != nil {
		fmt.Println(`redis  初始化失败！`)
	}
	utils.NewRedis(toml.RedisDb.Addr, toml.RedisDb.Port, toml.RedisDb.Password, toml.RedisDb.DB, toml.RedisDb.Pre, 0)
}

var wg sync.WaitGroup

func RedisPost() {
	//查询

	//g.DB(`post`).Model(`post`).
	//	Fields(`Title`,`HtmlContext`).
	//	Where(`aliyun_url`, "").
	//	Where(`CreateOn>=?`, `2022-05-8`).
	//	Structs(&post)
	//大批量查询
	/***
	  SELECT Id,CreateOn,post_html from post  where DATE_FORMAT(CreateOn, '%Y%m' ) = DATE_FORMAT( CURDATE() , '%Y%m' ) -- 本月

	  SELECT a.Id,a.html_context from post_html as a WHERE PERIOD_DIFF(date_format(now() ,'%Y%m') , date_format(a.CreateOn, '%Y%m')) =2
	  ORDER BY a.Createon asc -- 上一个月
	  ***/
	//SELECT a.Id,`html_context`,a.CreateOn from post_html as a WHERE PERIOD_DIFF(date_format(now() ,'%Y%m') , date_format(a.CreateOn, '%Y%m')) =0 AND html_context !=""
	//ORDER BY a.Createon DESC -- 上一个月
	sql := fmt.Sprintf("SELECT a.Id,`html_context`,a.CreateOn,a.post_id from post_html as a WHERE PERIOD_DIFF(date_format(now() ,'%Y%m') , date_format(a.CreateOn, '%Y%m')) " +
		"=0 AND html_context !='' AND IsUpdate = false ORDER BY a.Createon DESC")
	rows, err := g.DB(`post`).Query(sql)
	//var post []*model.PostModel

	for rows.Next() {
		var Id int
		var html_context, post_id string
		var datetime string
		err := rows.Scan(&Id, &html_context, &datetime, &post_id)
		if err != nil {
			//读取结果集失败
			panic(err)
		}
		//fmt.Println(`当前`, Id)
		//转换
		wg.Add(3)
		Url := ConvertHtmlToHref(html_context)
		if Url == "" {
			continue
		}
		//go func() {
		//	utils.SetSingleHashData(`Url_db`, post_id, Url, time.Hour*240)
		//}()
		//wg.Done()

		Url = fmt.Sprintf(`AliyunpanUrl="%s",IsUpdate=true`, Url)
		htmlUrl := fmt.Sprintf(`IsUpdate=%v`, true)
		//"AliyunpanUrl": gdb.Raw(Url),
		//fmt.Println(`当前转换出来的`, Url)
		go updatard(Url, post_id, Id, htmlUrl)
		wg.Done()
		//_, err = g.DB().Table(`postInfo`).FieldsEx(`id`).Update(, "id=?", old.Id)
	}
	//err := g.DB().GetStructs(&post, sql)
	if err != nil {
		fmt.Println(err)
	}
	//fmt.Println(`ddd`, post)
	//for _, node := range post {
	//	//fmt.Println(`时间`, node.CreateOn)
	//	fmt.Println(`-------------------`)
	//	//fmt.Println(`标题`, node.Title)
	//	fmt.Println(reflect.TypeOf(post))
	//	//re := regexp.MustCompile(`(https://www.aliyundrive.com/s.+?")`).
	//	//	FindAllStringSubmatch(node.HtmlContext, -1)
	//	//if re == nil {
	//	//	continue
	//	//}
	//	//fmt.Println(`长度`, len(re))
	//	//fmt.Println(`222`, re)
	//	//for index, hrefNode := range re {
	//	//	//fmt.Println(`当前:`, hrefNode[index])
	//	//	node.AlYunUrl = hrefNode[index]
	//	//}
	//	//arr := strings.ReplaceAll(node.AlYunUrl, `"`, ` `)
	//	//fmt.Println(`当前22:`, arr)
	//	//utils.PushListFromLeft(`AlYunUrl2`, node.AlYunUrl,node.Title)
	//	utils.SetSingleHashData(`test2`, node.Title, node.HtmlContext, time.Hour*24)
	//}

	//utils.PushListFromLeft(`tops`, "studentList", "lily", "lilei", "zhangsan", "lisi", "tom", "lisi", "laowang")

	//s1 := []string{"val2", "val3", "val4"}
	//bs, _ := json.Marshal(post)

}
func updatard(Url string, post_id string, Id int, htmlUrl string) {
	_, err := g.DB(`post`).Model(`post_thread`).Data(Url).
		Where("post_id", post_id).
		Where(`IsUpdate`, false).
		Update()

	_, err = g.DB(`post`).Model(`post_html`).Data(htmlUrl).
		Where("Id", Id).
		Where(`IsUpdate`, false).
		Update()
	if err != nil {
		fmt.Println(`err`, err)
	}
	wg.Wait()
	fmt.Println(`row`)
}

func UpdateHtml(Id int) {

	wg.Wait()
}

type Info struct {
	Name string
	Age  int
}

func ConvertHtmlToHref(posts string) string {
	re := regexp.MustCompile(`https://www.aliyundrive.com/s/[A-Za-z0-9]+"`).
		FindAllString(posts, -1)
	var result string
	if re != nil {
		for _, node := range re {
			node = strings.ReplaceAll(node, `"`, ``)
			result += node + ","
			//result += node
		}
		//posts.AlYunUrl = result
		return result
	}
	return ""
}

//func Insertdb() {
//	var rdb = utils.GetRedisClient()
//	vals, err := rdb.LRange(`Url_db`, 0, 2).Result()
//	if err != nil {
//		panic(err)
//	}
//	for node := range vals {
//		fmt.Println(`node`,node)
//	}
//
//}
